/*******************************************************************************
 * Copyright (c) 2013 Prometheus Consulting
 *
 *     Licensed under the Apache License, Version 2.0 (the "License"); you may
 *     not use this file except in compliance with the License. You may obtain
 *     a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *     Unless required by applicable law or agreed to in writing, software
 *     distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *     WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 *     License for the specific language governing permissions and limitations
 *     under the License.
 *******************************************************************************/
package nz.co.senanque.pizzaorder;

import java.util.Arrays;
import java.util.List;

import nz.co.senanque.pizzaorder.instances.OrderItem;
import nz.co.senanque.vaadinsupport.CommandExt;
import nz.co.senanque.vaadinsupport.MaduraPropertyWrapper;
import nz.co.senanque.vaadinsupport.MenuItemPainter;
import nz.co.senanque.vaadinsupport.SimpleButtonPainter;
import nz.co.senanque.vaadinsupport.SubmitButtonPainter;
import nz.co.senanque.vaadinsupport.application.MaduraSessionManager;

import com.vaadin.annotations.AutoGenerated;
import com.vaadin.data.util.BeanItem;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.MenuBar;
import com.vaadin.ui.MenuBar.MenuItem;
import com.vaadin.ui.VerticalLayout;

public class ProductLayout extends CustomComponent {

	@AutoGenerated
	private VerticalLayout mainLayout;
	@AutoGenerated
	private HorizontalLayout horizontalLayout_3;
	@AutoGenerated
	private VerticalLayout verticalLayout_3;
	@AutoGenerated
	private Button cancelButton;
	@AutoGenerated
	private Button orderButton;
	@AutoGenerated
	private ProductForm productForm_1;
	@AutoGenerated
	private MenuBar menuBar_1;
	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */
	private static final long serialVersionUID = -2770060727381961805L;
	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */
	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */
	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */
	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */
	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */
	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */
	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */
	private boolean m_newMode;
	private MainLayout m_owner;
	private MenuItem m_menuItemSave;

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

    private final MaduraSessionManager m_maduraSessionManager;
	public MaduraSessionManager getMaduraSessionManager() {
		return m_maduraSessionManager;
	}

	public MainLayout getOwner() {
		return m_owner;
	}
	/**
	 * The constructor should first build the main layout, set the
	 * composition root and then do any custom initialization.
	 *
	 * The constructor will not be automatically regenerated by the
	 * visual editor.
	 */
	public ProductLayout(MaduraSessionManager maduraSessionManager) {
		m_maduraSessionManager = maduraSessionManager;
		buildMainLayout();
		setCompositionRoot(mainLayout);

	}
	public void setOwner(MainLayout owner) {
		m_owner = owner;
		final MaduraSessionManager maduraSessionManager = getMaduraSessionManager();
		maduraSessionManager.register(cancelButton, new SimpleButtonPainter(m_maduraSessionManager));
		cancelButton.addListener(new ClickListener(){

			private static final long serialVersionUID = -1904914513136615107L;

			@Override
			public void buttonClick(ClickEvent event) {
				BeanItem<OrderItem> item = (BeanItem<OrderItem>)productForm_1.getItemDataSource();
				getOwner().cancel(item.getBean());
				getOwner().switchtoHome();
			}});
		maduraSessionManager.register(orderButton, new SubmitButtonPainter(m_maduraSessionManager));
		orderButton.addListener(new ClickListener(){

			private static final long serialVersionUID = 7776486822411822791L;

			@Override
			public void buttonClick(ClickEvent event) {
				getOwner().switchtoHome();
			}});
		// This is necessary for the order of fields. If a field named here does not exist then it is ignored, not an error
		productForm_1.setFieldList(Arrays.asList(new String[]{"description","base","size","grain","topping","testDate","testDouble","testing","amount"}));
		
		final MenuBar.MenuItem file = menuBar_1.addItem("File", null);
		m_menuItemSave = file.addItem("Save", new CommandExt(){

			private static final long serialVersionUID = 7142024506317299918L;
			MenuItemPainter m_menuItemPainter = new SubmitButtonPainter(m_maduraSessionManager);
			
			public void menuSelected(MenuItem selectedItem) {
				getOwner().switchtoHome();
			}

			public MenuItemPainter getPainter() {
				m_menuItemPainter.setProperties(productForm_1.getItemDataSourceProperties());
				return m_menuItemPainter;
			}

			public MaduraSessionManager getMaduraSessionManager() {
				return maduraSessionManager;
			}});
		maduraSessionManager.register(m_menuItemSave);
	}
	public void setNewMode(boolean b) {
		m_newMode = b;
		cancelButton.setEnabled(m_newMode);
	}

	public void setProduct(OrderItem pizza) {
		MaduraSessionManager maduraSessionManager = getMaduraSessionManager();
		productForm_1.setItemDataSource(new BeanItem<OrderItem>(pizza));
		List<MaduraPropertyWrapper> properties = productForm_1.getItemDataSourceProperties();
		maduraSessionManager.bind(orderButton, properties);
		maduraSessionManager.bind(cancelButton, properties);
		productForm_1.requestRepaint();
		maduraSessionManager.bind(m_menuItemSave, properties);
	}
	@AutoGenerated
	private VerticalLayout buildMainLayout() {
		// common part: create layout
		mainLayout = new VerticalLayout();
		mainLayout.setImmediate(false);
		mainLayout.setWidth("100%");
		mainLayout.setHeight("100%");
		mainLayout.setMargin(false);
		
		// top-level component properties
		setWidth("100.0%");
		setHeight("100.0%");
		
		// menuBar_1
		menuBar_1 = new MenuBar();
		menuBar_1.setImmediate(false);
		menuBar_1.setWidth("-1px");
		menuBar_1.setHeight("-1px");
		mainLayout.addComponent(menuBar_1);
		
		// horizontalLayout_3
		horizontalLayout_3 = buildHorizontalLayout_3();
		mainLayout.addComponent(horizontalLayout_3);
		
		return mainLayout;
	}
	@AutoGenerated
	private HorizontalLayout buildHorizontalLayout_3() {
		// common part: create layout
		horizontalLayout_3 = new HorizontalLayout();
		horizontalLayout_3.setImmediate(false);
		horizontalLayout_3.setWidth("-1px");
		horizontalLayout_3.setHeight("-1px");
		horizontalLayout_3.setMargin(false);
		
		// productForm_1
		productForm_1 = new ProductForm(m_maduraSessionManager);
		productForm_1.setImmediate(true);
		productForm_1.setWidth("360px");
		productForm_1.setHeight("-1px");
		horizontalLayout_3.addComponent(productForm_1);
		
		// verticalLayout_3
		verticalLayout_3 = buildVerticalLayout_3();
		horizontalLayout_3.addComponent(verticalLayout_3);
		
		return horizontalLayout_3;
	}
	@AutoGenerated
	private VerticalLayout buildVerticalLayout_3() {
		// common part: create layout
		verticalLayout_3 = new VerticalLayout();
		verticalLayout_3.setImmediate(false);
		verticalLayout_3.setWidth("-1px");
		verticalLayout_3.setHeight("-1px");
		verticalLayout_3.setMargin(false);
		
		// orderButton
		orderButton = new Button();
		orderButton.setCaption("orderButton");
		orderButton.setImmediate(true);
		orderButton.setWidth("-1px");
		orderButton.setHeight("-1px");
		verticalLayout_3.addComponent(orderButton);
		
		// cancelButton
		cancelButton = new Button();
		cancelButton.setCaption("cancelButton");
		cancelButton.setImmediate(true);
		cancelButton.setWidth("-1px");
		cancelButton.setHeight("-1px");
		verticalLayout_3.addComponent(cancelButton);
		
		return verticalLayout_3;
	}

}
